package cn.luowb.checkchat.controller;

import cn.luowb.checkchat.common.result.Result;
import cn.luowb.checkchat.common.util.TokenUtils;
import cn.luowb.checkchat.dao.entity.UserDO;
import cn.luowb.checkchat.dto.req.UserLoginRequest;
import cn.luowb.checkchat.dto.req.UserRegisterRequest;
import cn.luowb.checkchat.dto.req.UserUpdateRequest;
import cn.luowb.checkchat.dto.resp.LoginResponse;
import cn.luowb.checkchat.service.UserService;


import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result<UserDO> register(@Valid @RequestBody UserRegisterRequest request) {
        UserDO user = userService.register(request);
        return new Result<UserDO>()
                .setCode(Result.SUCCESS_CODE)
                .setMessage("注册成功")
                .setData(user);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<LoginResponse> login(@Valid @RequestBody UserLoginRequest request) {
        // 1. 校验用户名密码（由 UserService 实现）
        UserDO user = userService.verifyLogin(request);
        // 2. 生成 Token 并存储到 Redis
        String token = tokenUtils.generateToken(user.getUid());
        // 3. 构建登录响应
        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setToken(token);
        loginResponse.setUserInfo(user);

        return new Result<LoginResponse>()
                .setCode(Result.SUCCESS_CODE)
                .setMessage("登录成功")
                .setData(loginResponse);

    }

    /**
     * 获取用户信息
     * 注意：实际项目中需要通过token解析用户ID
     */
    @GetMapping("/info")
    public Result<UserDO> getUserInfo() {
        try {
            Long uid = tokenUtils.getUidFromToken(); // 从 Redis Token 中获取用户 ID
            UserDO user = userService.getUserInfo(uid);
            return new Result<UserDO>()
                    .setCode(Result.SUCCESS_CODE)
                    .setMessage("获取成功")
                    .setData(user);
        } catch (RuntimeException e) {
            return new Result<UserDO>()
                    .setCode("401")
                    .setMessage(e.getMessage());
        }

    }

    /**
     * 更新用户信息
     * 注意：实际项目中需要通过token解析用户ID
     */
    @PutMapping("/info")
    public Result<Void> updateUserInfo(@RequestBody UserUpdateRequest request) {
        try {
            Long uid = tokenUtils.getUidFromToken(); // 从 Redis Token 中获取用户 ID
            userService.updateUserInfo(uid, request);
            return new Result<Void>()
                    .setCode(Result.SUCCESS_CODE)
                    .setMessage("更新成功");
        } catch (RuntimeException e) {
            return new Result<Void>()
                    .setCode("401")
                    .setMessage(e.getMessage());
        }
    }
}